  <?php
  $auth = Auth::getAuth('current_user');
  $db = new Database();
  $company_id = $auth['company_id'];
  $search = new Search();
  if($auth['user_level_id']==2){
    $getcategory = $db->query("SELECT * FROM tbform_category WHERE company_id = $company_id", "array");
    array_push($getcategory,array("id"=>"0","category_name","Others"));
  }else{
    if(ALLOW_APPLICATION_PORTAL==0){
      $getcategory = $db->query("SELECT * FROM tbform_category WHERE company_id = $company_id", "array");
    }else{
      $category_id = $_COOKIE['application'];
      $getcategory = $db->query("SELECT * FROM tbform_category WHERE company_id = $company_id && id = $category_id", "array");
    }
    if($category_id==0){
      array_push($getcategory,array("id"=>"0","category_name","Others"));
    }
  }
  /*get visible application / form category*/
  $forms_array = $db->query("SELECT ws.form_name as form_name, ws.id as id, ws.form_json as form_json, ws.form_authors, ws.form_viewers 
                                            FROM tb_workspace ws
                                            LEFT JOIN tbworkflow wf on ws.id = wf.form_id
                                            WHERE wf.is_active = 1 and ws.is_active = 1
                                            AND company_id = {$db->escape($auth[company_id])} 
                                            ORDER BY ws.form_name DESC","array");
  $array_categoryID = array();
    foreach ($forms_array as $forms) {
      $json =  json_decode($forms['form_json'],true);
      $formID = $forms['id'];
      $formName = $forms['form_name'];
      $forms = $json['form_json'];
      $categoryID = $json['categoryName'];
      
     array_push($array_categoryID,$categoryID);
   }
   /* end get visible application / form category*/

   //get Categories
  foreach ($getcategory as $value) {
  
    
    $category_name = $value['category_name'];
    $category_id = $value['id'];
    if($category_id == 0){
      $category_name = "Others";
    }
    
   if(!in_array($value['id'],$array_categoryID)){
    continue;
   }
  if($value['users']!=""){
    //search if user in form category
    if($search->getFormPrivacyUsers($value['users'])==false){
      continue;
    }   
  }
  ?>
<div class="fl-option-toolbar">
    <div class="modules icon"></div>
    <div class="fl-toolbar-left-wrapper">
      <h1><?php echo $category_name; ?></h1>
    </div>
    <div class="fl-toolbar-right-wrapper">
        <!-- <a class="pinit icon dataTip" data-placement="left" data-original-title="Pin this to Dashboard"></a> -->
    </div>
     <div class="clearfix"></div>
  </div><!-- end of option toolbar -->
<div class="fl-module-category-wrapper">   
<ul class="fl-module-icon grid">
<?php
//get Forms 
foreach ($forms_array as $forms) {
                                       
   $json =  json_decode($forms['form_json'],true);
   $formID = $forms['id'];
   $formName = $forms['form_name'];
   // $forms = $json['form_json'];
   $categoryID = $json['categoryName'];
   $form_json = json_decode($json['form_json']);
   // segregate forms per form category
   if($categoryID==$category_id){
      // if($json['form_json']['form-users']!=null){
      //   if($search->getFormPrivacyUsers(json_encode($json['form_json']['form-users']))==false){
      //     continue;
      //   }
      // }
      $result = $search->getManyRequest("",0,$formID,0,1,array());
      $countReq = count($result);
      if(!$search->getFormPrivacyUsers($forms['form_authors']) && !$search->getFormPrivacyUsers($forms['form_viewers']) && $countReq==0){
        continue;
      }
      ?>
      <li>
        <a href="/user_view/application?id=<?php echo functions::base_encode_decode("encrypt",$formID); ?>">
        <figure>
          <div align="center"><img src="/images/icon/acct-icon/salesbook.png" alt=""></div>
            <figcaption>
              <div class="fl-icon-button">
                <br/>
                <!-- <h3><?php echo $formName; ?></h3> -->
                 <br/>
                <!-- <a href="/user_view/application?id=<?php echo functions::base_encode_decode("encrypt",$formID); ?>">View</a> -->
              </div>
            </figcaption>
        </figure>
        <div class="fl-mod-title"><?php echo $formName; ?></div>
        </a>
       </li>

      <?php
   }
}
?>
 <div class="clearfix"></div>
</ul>
</div>
<?php
}
?>